package team.seekers.oj.homework.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.oj.common.annotation.UserSession;
import team.seekers.oj.common.entity.ApiResponseBody;
import team.seekers.oj.common.entity.PageResult;
import team.seekers.oj.common.entity.UserSessionDTO;
import team.seekers.oj.common.exception.InternalApiException;
import team.seekers.oj.homework.dto.HomeworkDTO;
import team.seekers.oj.homework.dto.HomeworkListDTO;
import team.seekers.oj.homework.dto.HomeworkListReqDTO;
import team.seekers.oj.homework.service.HomeworkService;

import javax.validation.constraints.NotBlank;

@Controller
@RequestMapping("/homework")
public class HomeworkController {

    @Autowired
    private HomeworkService homeworkService;


    @GetMapping("/page")
    @ApiResponseBody
    public PageResult<HomeworkListDTO> list(HomeworkListReqDTO reqDTO,
                                            @UserSession(nullable=true) UserSessionDTO userSessionDTO) {
        if(userSessionDTO.getRoles().contains("user")){
            reqDTO.setStudentId(userSessionDTO.getUserId());
        }else if(userSessionDTO.getRoles().contains("admin")){
            reqDTO.setTeacherId(userSessionDTO.getUserId());
        }
        return homeworkService.page(reqDTO);
    }


    @GetMapping("/query")
    @ApiResponseBody
    public HomeworkDTO queryDetail(@RequestParam("contestId") @NotBlank Long homeworkId,
                                   @UserSession UserSessionDTO userSessionDTO) throws InternalApiException {
        HomeworkDTO homeworkDTO = homeworkService.query(homeworkId);
        return homeworkDTO;
    }
}